package com.example.service;

import com.example.dao.StudentDao;
import com.example.entity.PageResult;
import com.example.entity.Student;
import com.example.enums.ResultEnum;
import com.example.exception.StudentException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author John
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StudentService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StudentService.class);


    @Resource
    private RedisTemplate redisTemplate;


    @Resource
    private StudentDao studentDao;


    public PageResult pageShow(int limit, int offset) {
        /*Map<String, Object> map = new HashMap<>();
        List<Student> page = new ArrayList<>();
        List<Student> all = studentDao.all();
        for (int i = offset; i < offset + limit; i++) {
            if (i < all.size()) {
                page.add(all.get(i));
            }
        }
        map.put("rows", page);
        map.put("total", all.size());
        return map;*/


        PageHelper.startPage(limit,offset);
        List<Student> studentList = studentDao.all();
        Page<Student> page = (Page<Student>)studentList;
        return new PageResult(page.getTotal(),page.getResult());
    }


    public List<Student> all() {
        String key = "allStudent";
        ValueOperations<String, List<Student>> operations = redisTemplate.opsForValue();
        List<Student> list = operations.get(key);
        if (list != null) {
            LOGGER.info("StudentService.allStudent() : 从缓存中获取了学生 >> " + list.toString());
            return list;
        }
        List<Student> list1 = studentDao.all();
        operations.set(key, list, 10, TimeUnit.MINUTES);
        return list1;
    }


    public int insert(Student student) {
        Student student1 = studentDao.searchById(student.getId());
        if (student1 == null) {
            return studentDao.insert(student);
        } else {
            throw new StudentException(ResultEnum.INSERT_EROOR);
        }
    }

    public int deleteById(String id) {
        String key = "Student_" + id;
        ValueOperations<String, Student> operations = redisTemplate.opsForValue();
        Student student = operations.get(key);
        if (student == null) {
            if (studentDao.searchById(id) == null) {
                throw new StudentException(ResultEnum.DELETE_EROOR);
            } else {
                return studentDao.deleteById(id);
            }
        } else {
            redisTemplate.delete(key);
            LOGGER.info("StudentService.deleteStudent() : 从缓存中删除学生 >> " + student.toString());
            return studentDao.deleteById(id);
        }

    }


        /*Student student = studentDao.searchById(id);
        boolean haskey = redisTemplate.hasKey(key);
        if (haskey) {
            redisTemplate.delete(key);
            LOGGER.info("StudentService.deleteStudent() : 从缓存中删除学生 >> " + student.toString());
        }
        if (studentDao.searchById(id) == null) {
            throw new StudentException(ResultEnum.DELETE_EROOR);
        } else {
            return studentDao.deleteStudent(id);
        }*/


    public int update(String oldid, String id, String name, String sex, String age) {
        return studentDao.update(oldid, id, name, sex, age);
    }

    public Student searchById(String id) {
        ValueOperations<String, Student> operations = redisTemplate.opsForValue();
        String key = "Student_" + id;
        Student student = operations.get(key);
        if (student == null) {
            Student student1 = studentDao.searchById(id);
            if (student1 == null) {
                throw new StudentException(ResultEnum.SEARCH_EROOR);
            } else {
                operations.set(key, student1, 10, TimeUnit.MINUTES);
                return student1;
            }
        }
        LOGGER.info("StudentService.searchById() : 从缓存中获取了学生 >> " + student.toString());
        return student;
    }


}
